package com.example.mall.search.config;

import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MallElasticsearchConfiguration {

    @Bean
    public ElasticsearchClient elasticsearchClient() {
        return new ElasticsearchClient(getRestClientTransport());
    }

    @Bean
    public ElasticsearchAsyncClient elasticsearchAsyncClient() {
        return new ElasticsearchAsyncClient(getRestClientTransport());
    }

    @NotNull
    private RestClientTransport getRestClientTransport() {
        RestClient restClient = RestClient.builder(new HttpHost("192.168.88.130", 9200)).build();
        return new RestClientTransport(restClient, new JacksonJsonpMapper());
    }

}
